﻿namespace com.vs.General
{
    using Microsoft.Win32;
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Security.Cryptography;
    using System.Text;

    public class EncryptionManager
    {
        public static string DecryptData(string encryptedData)
        {
            string str5;
            try
            {
                string str;
                string str2;
                string str3;
                ASCIIEncoding encoding = new ASCIIEncoding();
                MemoryStream stream2 = new MemoryStream();
                MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
                GetEncryptionKey(out str, out str2);
                byte[] bytes = encoding.GetBytes(str);
                byte[] rgbKey = provider.ComputeHash(bytes);
                byte[] buffer4 = encoding.GetBytes(str2);
                byte[] rgbIV = provider.ComputeHash(buffer4);
                SymmetricAlgorithm symmetricAlgorithm = GetSymmetricAlgorithm();
                symmetricAlgorithm.Padding = PaddingMode.Zeros;
                symmetricAlgorithm.Mode = CipherMode.CBC;
                CryptoStream stream = new CryptoStream(stream2, symmetricAlgorithm.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                byte[] buffer = Convert.FromBase64String(encryptedData);
                stream.Write(buffer, 0, buffer.Length);
                stream.FlushFinalBlock();
                stream.Close();
                string str4 = encoding.GetString(stream2.ToArray());
                stream2.Close();
                int length = str4.IndexOf("\0", 0, str4.Length);
                if (length > 0)
                {
                    str3 = str4.Substring(0, length);
                }
                else
                {
                    str3 = str4;
                }
                str5 = str3;
            }
            catch (Exception)
            {
                throw new Exception(MessageManager.MSG_LIC_005);
            }
            return str5;
        }

        public static string DecryptDataFromFile(string stFileName, byte[] rijnKey, byte[] rijnIV)
        {
            string str3;
            int length = 0;
            string str = null;
            string str2 = null;
            try
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                FileStream stream2 = new FileStream(stFileName, FileMode.Open, FileAccess.Read);
                length = (int) stream2.Length;
                MemoryStream stream3 = new MemoryStream();
                byte[] buffer2 = new byte[100];
                long num2 = 0;
                long num3 = stream2.Length;
                SymmetricAlgorithm symmetricAlgorithm = GetSymmetricAlgorithm();
                symmetricAlgorithm.Padding = PaddingMode.Zeros;
                CryptoStream stream = new CryptoStream(stream3, symmetricAlgorithm.CreateDecryptor(rijnKey, rijnIV), CryptoStreamMode.Write);
                while (num2 < num3)
                {
                    int count = stream2.Read(buffer2, 0, 100);
                    stream.Write(buffer2, 0, count);
                    num2 += count;
                }
                stream.Close();
                stream2.Close();
                byte[] bytes = stream3.ToArray();
                str2 = encoding.GetString(bytes);
                int num5 = str2.IndexOf("\0", 0, str2.Length);
                if (num5 > 0)
                {
                    str = str2.Substring(0, num5);
                }
                else
                {
                    str = str2;
                }
                str3 = str;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str3;
        }

        public string DecryptDataString(string encryptedData)
        {
            return DecryptData(encryptedData);
        }

        public static string EncryptData(string plainText)
        {
            string str4;
            try
            {
                string str;
                string str2;
                ASCIIEncoding encoding = new ASCIIEncoding();
                MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
                MemoryStream stream = new MemoryStream();
                GetEncryptionKey(out str, out str2);
                byte[] bytes = encoding.GetBytes(str);
                byte[] rgbKey = provider.ComputeHash(bytes);
                byte[] buffer4 = encoding.GetBytes(str2);
                byte[] rgbIV = provider.ComputeHash(buffer4);
                SymmetricAlgorithm symmetricAlgorithm = GetSymmetricAlgorithm();
                symmetricAlgorithm.Padding = PaddingMode.Zeros;
                symmetricAlgorithm.Mode = CipherMode.CBC;
                CryptoStream stream2 = new CryptoStream(stream, symmetricAlgorithm.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                byte[] buffer = encoding.GetBytes(plainText);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                stream2.Close();
                string str3 = Convert.ToBase64String(stream.ToArray());
                stream.Close();
                str4 = str3;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str4;
        }

        public static string EncryptData(EncryptionType encryptionType, string value)
        {
            try
            {
                SHA512Managed managed = new SHA512Managed();
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] bytes = managed.ComputeHash(encoding.GetBytes(value));
                return encoding.GetString(bytes);
            }
            finally
            {
            }
        }

        public string EncryptDataString(string data)
        {
            return EncryptData(data);
        }

        public static void EncryptDataToFile(string inputData, string outName, byte[] rijnKey, byte[] rijnIV)
        {
            try
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                int count = 0;
                FileStream stream = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
                stream.SetLength(0);
                SymmetricAlgorithm symmetricAlgorithm = GetSymmetricAlgorithm();
                symmetricAlgorithm.Padding = PaddingMode.Zeros;
                CryptoStream stream2 = new CryptoStream(stream, symmetricAlgorithm.CreateEncryptor(rijnKey, rijnIV), CryptoStreamMode.Write);
                byte[] bytes = encoding.GetBytes(inputData);
                count = bytes.Length;
                stream2.Write(bytes, 0, count);
                stream2.Close();
                stream.Close();
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        public static string GetDecryptedSecurityKey(string data)
        {
            byte[] bytes = ProtectedData.Unprotect(Convert.FromBase64String(data), null, DataProtectionScope.LocalMachine);
            return Encoding.UTF8.GetString(bytes);
        }

        private static void GetEncryptionKey(out string secKey, out string IV)
        {
            object config = ConfigManager.GetConfig(LicenseKeyConstants.stCONFIG_SECURITYKEY_NODE);
            object obj3 = ConfigManager.GetConfig(LicenseKeyConstants.stCONFIG_IV_NODE);
            if ((config == null) || (obj3 == null))
            {
                throw new Exception("Security Key/IV not found");
            }
            secKey = GetDecryptedSecurityKey(config.ToString());
            IV = GetDecryptedSecurityKey(obj3.ToString());
        }

        private static string GetSymmetricAlgoNameFromRegistry()
        {
            RegistryKey key = null;
            using (key = Registry.LocalMachine.OpenSubKey(com.vs.General.Constants._REGISTRYPATH))
            {
                if ((key != null) && (key.GetValue("SymmetricAlgoName") != null))
                {
                    return key.GetValue("SymmetricAlgoName").ToString();
                }
            }
            return string.Empty;
        }

        private static SymmetricAlgorithm GetSymmetricAlgorithm()
        {
            string symmetricAlgoNameFromRegistry = GetSymmetricAlgoNameFromRegistry();
            if (!StringManager.IsEmpty(symmetricAlgoNameFromRegistry))
            {
                return SymmetricAlgorithm.Create(symmetricAlgoNameFromRegistry);
            }
            return SymmetricAlgorithm.Create();
        }

        public static bool IsMatch(string value, string encryptedValue)
        {
            return string.Equals(encryptedValue, EncryptData(value));
        }
    }
}

